import json
import time
import requests
import re


def query(num):
    with open('cookie.json', 'r',encoding='utf-8') as f:
        headers = json.load(f)

    params = {
        'isprivilege': '',
        'from': '',
        'isFastSearch': 'false',
        'jumpState': '',
    }

    data = {
        'applicant': '',
        'applynum': num,
        'itemname': '',
        'itemType': '',
        'receiveUser': '',
        'receiveTime': '2025-01-01 至 2099-08-18',
        'receiveTime_start': '2025-01-01',
        'receiveTime_end': '2099-08-18',
        'acceptUser': '',
        'submitTime': '',
        'submitTime_start': '',
        'submitTime_end': '',
        'finishTime': '',
        'finishTime_start': '',
        'finishTime_end': '',
        'regName': '',
        'regCode': '',
        'regId': '',
        'orgName': '',
        'orgCode': '',
        'applySubject': '',
        'curState': '',
        'state': '',
        'applyFrom': '',
        'assort': '',
        'objectCertNo': '',
        'agentIdCard': '',
        'queryDepartment': '0',
        'serviceObject': '',
        'companyRegisterRegion': '',
        'isFinish': '2',
        '_search': 'true',
        'nd': int(time.time() * 1000),
        'rows': '10',
        'page': '1',
        'sidx': 'FINISH_TIME DESC, RECEIVE_TIME ',
        'sord': 'desc',
    }

    response = requests.post(
        'http://59.215.206.188/zhslzy/accept/monitor/allbusiness/queryAll',
        params=params,

        headers=headers,
        data=data,

    ).json()
    BSNUM=response['rows'][0]['cell']['BSNUM']
    REGION_CODE	=response['rows'][0]['cell']['REGION_CODE']

    response2 = requests.get(
        f'http://59.215.206.188/zhslzy/accept/business/completework/showDetail?bsnum={BSNUM}&type=&source=&_={int(time.time() * 1000)}',

        headers=headers,
    ).text
    # print(response)

    request_body='''
{
"accessToken":"11",
"xmlStr":"<?xml version=\"1.0\" encoding=\"UTF-8\"?><APPROVEDATAINFO><SBLSH_SHORT>流水号</SBLSH_SHORT><SXBM>事项编码</SXBM><XZQHDM>区划</XZQHDM><EXPRESSTYPE>3</EXPRESSTYPE><YWLY/><BZSLBLRXM/><BZCLQD/><BZSJ/><SENDTODOMESSAGE/><EXCHANGENO/></APPROVEDATAINFO>"
}
    '''
    # 流水号 = re.findall('<SBLSH_SHORT>(.*?)</SBLSH_SHORT>', request_body)[0]

    事项编码 = re.findall('itemCode=(.*?)&receiveNumber', response2)[0]
    # 区划 = re.findall('<XZQHDM>(.*?)</XZQHDM>', request_body)[0]
    request_bodys=request_body.replace('流水号', num).replace('事项编码',事项编码).replace('区划',REGION_CODE)
    print(request_bodys)
    # import test
    # res=test.extract_and_format_xml_from_json(request_bodys)
    # print(res)

list=[
    "5203297660FXWZ20251126592623",


]
for i in list:
    query(i)


# 使用的是综合受理59的cookie
# 使用的是综合受理59的cookie
# 使用的是综合受理59的cookie